package cn.edu.nju.askinginhand.util;

import java.net.URI;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonParserTest3 {
	//private static String url = "http://10.0.2.2:8080/OralDefenseSystem/teacher/teacherListAction";
	private static String url = "http://192.168.0.119:8080/OralDefenseSystem/teacher/teacherListAction";

	/**TODO : Handle Exception
	 */
	
	public static String getRootURL() {
		return url;
	}
	/**
	 * @param url
	 * @throws Exception
	 */
	public static String getPDAServerData() throws Exception {
		System.out.println("JsonParserTest3.getPDAServerData()");
		String result = null;
		HttpClient client = new DefaultHttpClient();
		HttpPost request;
		//测试登录
		request = new HttpPost(new URI(url));
		System.out.println(url);
		HttpResponse response = client.execute(request);
		System.out.println(response.getStatusLine());
		if (response.getStatusLine().getStatusCode() == 200) { // 200
			System.out.println("status 200");
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String out = EntityUtils.toString(entity);
				try {
					List<Map<String, String>> list = JSONParser.getList(out);
					System.out.println("List.get(0)"+list.get(0));
					result = (String)(list.get(0).get("name"));
					/*
					 * String src = "{\"strResult\":\"no such username\"}";
					 * String result = null; JSONObject jsonObject2 = new
					 * JSONObject(src);
					 * System.out.println("object2:"+jsonObject2
					 * .get("strResult"));
					 */} catch (Exception e) {
						 System.out.println("exception");
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
}
